package com.twu.biblioteca.option;

import com.twu.biblioteca.core.Movie;
import com.twu.biblioteca.service.MovieService;
import com.twu.biblioteca.core.Option;
import com.twu.biblioteca.util.Cli;

public class MovieOption extends Option {

    public static final String FORMAT = "|%1$-20s|%2$-4s|%3$-20s|%4$-3s|\n";
    private final MovieService movieService;

    public MovieOption(MovieService movieService, Cli cli) {
        super("Movie", cli);
        this.movieService = movieService;
    }

    public String displayTitle() {
        return String.format(FORMAT, "Movie", "Year", "Director", "Rating");
    }

    public String displayMovie(Movie movie) {
        return String.format(FORMAT, movie.getName(), movie.getYear(), movie.getDirector(), movie.getRating());
    }

    @Override
    public void execute() {
        cli.output(displayMovies());
    }

    public String displayMovies() {
        StringBuilder result = new StringBuilder();
        result.append(displayTitle());
        for (Movie movie : movieService.getMovies()) {
            result.append(displayMovie(movie));
        }
        return result.toString();
    }
}
